package edu.xidian.bdilab.jrbigdata.service.enterprise.productRequest.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.enterprise.EnterpriseCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.productRequest.ProductRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName ProductRequestServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/4/2 上午10:47
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class ProductRequestServiceImpl implements ProductRequestService {
    @Autowired
    ProductRequestMapper productRequestMapper;
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    @Override
    public ResponseResult setRequestInfo(String socialCode, String productName, String fundUsage) throws ParseException {
        ResponseResult responseResult = new ResponseResult();

        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_social_code", socialCode)
                                  .eq("fk_product_name", productName);
        ProductRequest oldProductRequest = productRequestMapper
                .selectOne(productRequestQueryWrapper);
        if(oldProductRequest!=null)
        {
            responseResult.setData("已经申请过对应商品");
            responseResult.setCode(EnterpriseCode.PRODUCT_REQUEST_FAIL.getCode());
            responseResult.setMessage(EnterpriseCode.PRODUCT_REQUEST_FAIL.getMessage());
            responseResult.setSuccess(false);
            return responseResult;
        }
        QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper = new QueryWrapper<>();
        enterpriseInfoQueryWrapper.eq("social_code", socialCode);
        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(enterpriseInfoQueryWrapper);

        ProductRequest productRequest = new ProductRequest();

        productRequest.setFkProductName(productName);
        productRequest.setFkSocialCode(socialCode);
        productRequest.setEnterpriseName(enterpriseInfo.getEnterpriseName());
        productRequest.setStatus(0);
        productRequest.setComment("暂无进度");
        if(fundUsage!=null&&fundUsage.length()!=0)
        {   productRequest.setFundUsage(fundUsage); }
        else{
            productRequest.setFundUsage("暂未说明用途");
        }

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String temp = dateFormat.format(date);

        productRequest.setRequestTime(dateFormat.parse(temp));
        productRequestMapper.insert(productRequest);

        responseResult.setCode(EnterpriseCode.PRODUCT_REQUEST_SUCCESS.getCode());
        responseResult.setMessage(EnterpriseCode.PRODUCT_REQUEST_SUCCESS.getMessage());
        responseResult.setSuccess(false);
        return responseResult;

    }
}
